package com.yunquan.ohana.ui.calendar.lists

import android.os.Bundle
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivityListsBinding
import com.yunquan.ohana.entity.FramesDataManager
import com.yunquan.ohana.ui.calendar.event.dialog.EventSyncDialog
import com.yunquan.ohana.ui.calendar.lists.adapter.ListsAdapter
import com.yunquan.ohana.ui.scan.ScanActivity
import java.util.Locale

class ListsActivity : BaseActivity<ActivityListsBinding, ListsViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_lists
    }

    override fun initVariableId(): Int {
        return BR.listsViewModel
    }

    override fun initParam() {
        super.initParam()
        Locale.setDefault(Locale("en"))
        // 设置背景颜色
        window.statusBarColor = ContextCompat.getColor(this, R.color.white)
        // 设置文字颜色（true = 黑色文字，false = 白色文字）
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = true
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        binding!!.adapter = ListsAdapter()
        val bundle = intent.extras
        bundle?.let {
            viewModel!!.mDeviceId = it.getString("deviceId")
            viewModel!!.isUserType.set(it.getBoolean("userType", false))
        }
        viewModel!!.getListsGroup()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        binding!!.smartRefreshLayout.setEnableRefresh(false)
        binding!!.smartRefreshLayout.setEnableLoadMore(false)
        binding!!.smartRefreshLayout.setOnRefreshListener {
            viewModel!!.getListsGroup()
        }
        binding!!.tvSync.setOnClickListener {
            val deviceList = FramesDataManager.instance.getData().filter {
                it.id != viewModel!!.mDeviceId
            }.toMutableList()
            val dialog = EventSyncDialog(
                context = this,
                mActivityResult,
                devices = deviceList,
            )
            dialog.setOnBindDeviceListener { code ->
                viewModel!!.bindFrames(code)
            }
            dialog.setOnSelectDeviceListener { devices ->
                viewModel!!.syncToDevice(devices)
            }
            dialog.show()
        }
    }

    override fun onActivityResult(result: ActivityResult) {
        super.onActivityResult(result)
        if (result.resultCode == 1005) {
            viewModel!!.getListsGroup()
        }
    }

    // 这里处理返回结果
    private val mActivityResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == ScanActivity.RESULT_CODE) {
            // 扫描二维码返回
            val bundle = result.data?.extras
            val text = bundle?.getString("text")
            text?.let {
                viewModel!!.bindFrames(it)
            }
        }
    }
}